package com.epfresh.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.epfresh.R;
import com.epfresh.api.widget.tablayout.CommonTabLayout;
import com.epfresh.api.widget.tablayout.TabEntity;
import com.epfresh.api.widget.tablayout.listener.OnTabSelectListener;
import com.epfresh.fragment.OrdersListFragment;
import com.epfresh.global.BaseActivity;

import java.util.ArrayList;

public class MyOrderActivity extends BaseActivity {
    private CommonTabLayout mTabLayout;
    private ViewPager mViewPager;
    private final int TAB_COUNT = 5;
    Context mContext;
    public static String id="";
    int pageFlag=0;//全部订单的页面逻辑可以和其他页面区别优化

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }
    OrdersListFragment[] fragments = new OrdersListFragment[5];

    public void initView(){
        setContentView(R.layout.fragment_my_order);
        mContext = this;
        righticon.setImageResource(R.mipmap.order_search);
        righticon.setVisibility(View.VISIBLE);
        righticon.setOnClickListener(this);
        basetitle.setText("我的订单");
        baseback.setVisibility(View.VISIBLE);
        mTabLayout = (CommonTabLayout) findViewById(R.id.id_tablayout);
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        setAdapter();
        mViewPager.setCurrentItem(getIntent().getIntExtra("type",0));
        mViewPager.setOffscreenPageLimit(1);
    }

    public void setAdapter(){
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                if(fragments[i]==null){
                    fragments[i] = new OrdersListFragment(i);
                    return fragments[i];
                }else {
                    return fragments[i];
                }
            }

            @Override
            public CharSequence getPageTitle(int position) {
                switch (position) {
                    case 0:
                        return "全部";
                    case 1:
                        return "待付款";
                    case 2:
                        return "待发货";
                    case 3:
                        return "待收货";
                    case 4:
                        return "待评价";
                }
                return "Helloworld";
            }

            @Override
            public int getCount() {
                return TAB_COUNT;
            }
        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mTabLayout.setCurrentTab(position);
                if(fragments[position]==null){
                    return;
                }else {
                    fragments[position].intoFresh();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        String[] mTitles = {"全部", "待付款", "待发货", "待收货","待评价"};
        ArrayList<TabEntity> mTabEntities = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], 0, 0));
        }

        mTabLayout.setTabData(mTabEntities);
        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.righticon:
                Intent search = new Intent(this, OrderSearchActivity.class);
                startActivity(search);
                break;
            case R.id.baseback:
                if(getIntent().getBooleanExtra("homeFlag",false)){
                    intoHomePage(0);
                    finish();
                    return;
                }
                finish();
                break;
        }
    }
    @Override
    public void onBackPressed() {
        if(getIntent().getBooleanExtra("homeFlag",false)){
            intoHomePage(0);
            finish();
            return;
        }
        finish();
    }

    private void intoHomePage(int tabFlag) {
        Intent intent = new Intent(this,
                HomeActivity.class);
        intent.putExtra("home_tab_index", tabFlag);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK && requestCode == 0x001) {
            if (data != null) {
                int page = data.getIntExtra("page",0);
                mViewPager.setCurrentItem(page);
                if(fragments[page]!=null) {
                    fragments[page].intoFresh();
                }else{
                    initView();
                }
            }
        }
    }
}
